package com.java.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.java.config.result.Result;
import com.java.model.user.UserInfo;
import com.java.model.vo.UserInfoQueryVo;
import com.java.service.UserInfoService;
import com.java.utils.AddressUtils;
import com.java.utils.HttpClientUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

@Api("用户信息")
@RestController
@RequestMapping("/admin/user")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    //用户列表(条件查询分页)
    @ApiOperation("用户列表")
    @GetMapping("/{pageNum}/{pageSize}")
    public Result list(
            @PathVariable Long pageNum,
            @PathVariable long pageSize, UserInfoQueryVo userInfoQueryVo){

        Page<UserInfo> pageParam = new Page<>(pageNum, pageSize);

        IPage<UserInfo> page = userInfoService.selectPage(pageParam,userInfoQueryVo);

        return Result.ok(page);
    }


    //用户锁定
    @ApiOperation("用户锁定")
    @GetMapping("/lock/{userId}/{status}")
    public Result lock(@PathVariable Long userId,@PathVariable Integer status){
        userInfoService.lock(userId,status);
        return Result.ok();
    }

    //用户详情
    @ApiOperation("用户详情")
    @GetMapping("/show/{userId}")
    public Result show(@PathVariable Long userId){
      Map<String,Object> map = userInfoService.show(userId);
      return Result.ok(map);
    }

    //认证审批
    @ApiOperation("认证审批")
    @GetMapping("/approval/{userId}/{authStatus}")
    public Result approval(@PathVariable Long userId,@PathVariable Integer authStatus){
        userInfoService.approval(userId,authStatus);
        return Result.ok();
    }

    @ApiOperation("用户地理定位")
    @GetMapping("/getLocal")
    public String getLocal(HttpServletRequest request) throws Exception {
        Map<String , String > map = new HashMap<>();
        //String ip = request.getRemoteAddr();
        String ip = "222.240.205.12";
        map.put("ak","LT5dqaVdgKq213ZmwnTXmfFRBfCYjok9");
        map.put("ip",ip);
        map.put("coor","bd09ll");
        String parameters = HttpClientUtils.postParameters("https://api.map.baidu.com/location/ip", map);
        return parameters;
    }
}
